package com.abel.demo.config.security;

import com.abel.demo.utils.SignUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

import java.security.NoSuchAlgorithmException;

/**
 * Author: Abel.lin
 * Date: Created in 2018/7/13 14:47
 * Company: Abel.Studio
 * Copyright: Copyright (c) 2017
 * Description:
 */
@Configuration
//@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private MyFilterSecurityInterceptor myFilterSecurityInterceptor;

    @Bean
    UserDetailsService userSecurityService(){
        return new UserSecurityService();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userSecurityService()).passwordEncoder(new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                try {
                    return SignUtil.MD5((String)charSequence);
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
                return (String)charSequence;
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                try {
                    return s.equals(SignUtil.MD5((String)charSequence));
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
                return false;
            }
        });
    }

/*    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/static/**");
        web.ignoring().antMatchers("/login");
    }*/

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
            .antMatchers("/login","/user/login","/static/**").permitAll()     // 设置所有人都可以访问登录页面
//            .anyRequest().authenticated()
            .anyRequest().fullyAuthenticated()
        .and()
            .formLogin()
                .loginPage("/login")           // 设置登录页面
                .failureUrl("/login?error")
                .loginProcessingUrl("/user/login")  // 自定义的登录接口
                .permitAll()
        .and()
            .csrf().disable();          // 关闭csrf防护

        http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class);
    }
}
